/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.deployment.helper;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Device;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Node;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.deployment.Activator;
import com.cea.papyrus.diagram.deployment.preference.DeploymentDiagramPreferenceConstants;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;


/**
 * Defines management of devices.
 * 
 * @author Gilles Cannenterre
 * @since 15 janv. 07
 */
public class NodeDeviceHelper extends NodeNodeHelper{

	/**
	 * The channel.
	 */
	private int channel = 203;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  Element createUMLElement(String name, Element umlParent){
		Debug.debug(this,"createUMLElement(String,Element)", channel); 
		Device aDevice = null;
		if (umlParent instanceof Node) {
			aDevice = UMLFactory.eINSTANCE.createDevice();
			aDevice.setName(name);
			((Node) umlParent).getNestedNodes().add(aDevice);			
		} else if (umlParent instanceof Package) {
			aDevice = UMLFactory.eINSTANCE.createDevice();
			aDevice.setName(name);
			((Package) umlParent).getPackagedElements().add(aDevice);	
		} else {
			aDevice = UMLFactory.eINSTANCE.createDevice();
			aDevice.setName(name);
			((Package)umlParent.getNearestPackage()).getPackagedElements().add(aDevice);
		}
		return aDevice;		
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#getBaseString()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public String getBaseString() {
		return "Device_"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#setGraphElementPreferences(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * 
	 * 
	 * @param graphElement 
	 */
	@Override
	public void setGraphElementPreferences(GraphElement graphElement) {	
		// Get the preference store
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		
		// Set colors and font
		graphElement.setBackgroundColor(DeploymentDiagramPreferenceConstants.getPreferenceDeviceBackgroundColor(store));
		graphElement.setBorderColor(DeploymentDiagramPreferenceConstants.getPreferenceDeviceBorderColor(store));
		graphElement.setFontColor(DeploymentDiagramPreferenceConstants.getPreferenceDeviceFontColor(store));
		FontData[] fontdata = DeploymentDiagramPreferenceConstants.getPreferenceDeviceFont(store);
		if (fontdata.length>0){
			graphElement.setFontFamily(fontdata[0].getName());
			graphElement.setFontSize(fontdata[0].getHeight());
		}
	}
}